package com.soulkun.smarthome.ui.main.personal

import android.content.Intent
import android.view.View
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainPersonalBinding
import com.soulkun.smarthome.logic.enum.ErrorCodeEnum
import com.soulkun.smarthome.logic.exception.ApiException
import com.soulkun.smarthome.ui.main.MainNavigationDataBindingFragment
import com.soulkun.smarthome.ui.main.personal.dialog.AppMainPersonalRenameDialog
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalViewModel
import com.soulkun.smarthome.ui.service.WebSocketService
import com.soulkun.smarthome.ui.splash.AppSplashActivity
import soulkun.library.utils.logE
import soulkun.library.utils.navigateSafe
import soulkun.library.utils.screen.ScreenUtils
import soulkun.library.utils.toast

class AppMainPersonalFragment : MainNavigationDataBindingFragment<AppFragmentMainPersonalBinding>(){

    private val viewModel by viewModels<AppMainPersonalViewModel> { defaultViewModelProviderFactory }

    private val renameDialog by lazy {
        AppMainPersonalRenameDialog(viewModel)
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_personal
    }

    override fun initView() {
        super.initView()
        ScreenUtils.setViewFitSystemWindows(requireDataBinding().root)
    }

    override fun initData() {
        requireDataBinding().lifecycleOwner = viewLifecycleOwner
        requireDataBinding().viewModel = viewModel
        requireDataBinding().onClickListener = View.OnClickListener {
            when (it.id) {
                R.id.tv_main_personal_text_nickname -> {
                    renameDialog.show(childFragmentManager, "RenameDialog")
                }
                R.id.view_main_personal_background_family -> {
                    findNavController().navigateSafe(R.id.action_appMainPersonalFragment_to_appMainPersonalFamilyFragment, R.id.appMainPersonalFragment)
                }
                R.id.view_main_personal_background_message -> {
                    findNavController().navigateSafe(R.id.action_appMainPersonalFragment_to_appMainPersonalMessageFragment, R.id.appMainPersonalFragment)
                }
                R.id.view_main_personal_background_logout -> {
                    viewModel.logout().observe(viewLifecycleOwner){ result ->
                        if (result.isSuccess) {
                            result.getOrNull()!!.let { response ->
                                response.statusMessage.toast()
                                mActivity.stopService(Intent(mActivity, WebSocketService::class.java))
                                AppSplashActivity.startIntentAnimationSlideVertical(mActivity)
                                mActivity.finish()
                            }
                        }else{
                            result.exceptionOrNull()!!.let { throwable ->
                                val apiException = throwable as ApiException
                                if (apiException.errorCode == ErrorCodeEnum.AuthenticationError.code){
                                    "登出账号成功".toast()
                                    mActivity.stopService(Intent(mActivity, WebSocketService::class.java))
                                    AppSplashActivity.startIntentAnimationSlideVertical(mActivity)
                                    mActivity.finish()
                                }
                            }
                        }
                    }
                }
            }
        }
        viewModel.getUserInfo()
    }

    override fun initListener() {
    }

    override fun doOnCreate() {
    }

}